package com.jt.demo2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringGetDog {
    //该类表示从spring容器中，动态获取Dog对象
    public static void main(String[] args) {
        //1.指定Spring配置文件路径
        String resource="spring.xml";

        //2.启动Spring容器
ApplicationContext context=
        new ClassPathXmlApplicationContext(resource);
        //3.从容器中获取对象
        //通过ID获取
        Dog dog =(Dog) context.getBean("dog");
        //通过class获取
        Dog dog1 = context.getBean(Dog.class);
        //输出地址
        System.out.println(dog);
        System.out.println(dog1);
        //4.对象调用方法
        dog.hello();
//        dog2.hello();
        //利用反射机制获取对象
        getDog();
    }
    public static void getDog(){
        try {
         Dog dog = (Dog) Class.forName("com.jt.demo2.Dog").newInstance();
            dog.hello();




        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}